﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.ESS.BLL;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.DAL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MoneyRequestGuarantees : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MAS_BorrowTypes bt = new B_FIM_MAS_BorrowTypes();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        B_MessageSend send = new B_MessageSend();
        IBLL_FIM_MAS_Events ev = new B_FIM_MAS_Events();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();
                //===========================

                UpdateUI1("ViewAll");

                //loadDDLFilterEventSeq();
                LoadDDLFilterBgmSourceSeq();
                LoadDDLFilterWorkAreaSeq();
                lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
                rblEventSeq_SelectedIndexChanged(null, null);
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strEventSeq = ddlFilterEventSeq.SelectedValue;
            string strContactSeq = null;
            //string strEmpSeq = Session["EmpSeq"].ToString();
            RadGrid1.DataSource = mr.GetFull(strBgmSourceSeq, strBgmYear, strWorkAreaSeq, strEventSeq
                , strContactSeq, null, null, false);
        }

        //protected void loadDDLFilterEventSeq()
        //{
        //    ddlFilterEventSeq.DataSource = ev.GetList();
        //    ddlFilterEventSeq.DataBind();

        //    main.InsertEmptyDDLAll(ddlFilterEventSeq);
        //    ddlFilterEventSeq.SelectedIndex = 0;
        //}

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();

            ddlFilterBgmSourceSeq.SelectedIndex = 0;
        }

        protected void LoadDDLFilterWorkAreaSeq()
        {
            ddlFilterWorkAreaSeq.DataSource = area.GetFull();
            ddlFilterWorkAreaSeq.DataBind();

            ddlFilterWorkAreaSeq.SelectedIndex = 0;
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    PanelHeader.Visible = true;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    PanelHeader.Visible = false;
                    rblEventSeq.Enabled = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    PanelHeader.Visible = false;
                    rblEventSeq.Enabled = false;
                    break;
            }
        }

        protected void ClearControl1()
        {
            
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            string strBgmMonth;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;

            strBgmMonth = main.GetBgmMonth(strBgmSourceSeq, DateTime.Today).ToString();
            
            UpdateUI1("AddNew");
            //FIM_MoneyRequestDtls1.LoadRoot("-1", ddlFilterBgmSourceSeq.SelectedValue, txtBgmYear.Text, strBgmMonth, 400, 600);
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strMoneyReqSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_seq"].Text;
                string strEventSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["event_seq"].Text;

                rblEventSeq.SelectedValue = strEventSeq;
                EventChanged(strMoneyReqSeq);
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strMoneyReqSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_seq"].Text;

                info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                mr.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            //switch (rblEventSeq.SelectedValue)
            //{
            //    case "11":
            //        MoneyRequestDN1.Insert();
            //        break;
            //    case "12" :
            //        MoneyRequest1.Update();
            //        break;
            //    case "14" :
            //        MoneyRequestSalaries1.Insert();
            //        break;
            //    case "16" :
            //        MoneyRequestLoan1.Insert();
            //        break;
            //}

            UpdateUI1("Insert");
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            //switch (rblEventSeq.SelectedValue)
            //{
            //    case "11":
            //        MoneyRequestDN1.Update();
            //        break;
            //    case "12":
            //        MoneyRequest1.Update();
            //        break;
            //    case "14":
            //        MoneyRequestSalaries1.Update();
            //        break;
            //    case "16":
            //        MoneyRequestLoan1.Update();
            //        break;
            //}

            //UpdateUI1("Update");
            //LoadRadGrid1();
            //RadGrid1.DataBind();
        }

        protected void cmdPrint1_Click(object sender, EventArgs e)
        {
            //if (RadGrid1.SelectedItems.Count > 0)
            //{
            //    string strSeq = Session["ReceiveSeq"].ToString();

            //    main.SetupReport("Receipt", strSeq);
            //}
        }

        protected void rblEventSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            EventChanged("");
        }

        protected void EventChanged(string strMoneyReqSeq)
        {
            //string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            //string strBgmYear = txtBgmYear.Text;
            ////string strBgmMonth = main.GetBgmMonth(main.WebString(strBgmSourceSeq), Convert.ToDateTime(txtMoneyReqDate.TextWithLiterals)).ToString();

            //switch (rblEventSeq.SelectedValue)
            //{
            //    case "11"://ซื้อ/จ้าง
            //        MoneyRequestDN1.Visible = true;
            //        MoneyRequestSalaries1.Visible = false;
            //        MoneyRequestLoan1.Visible = false;
            //        MoneyRequest1.Visible = false;
            //        break;
            //    case "12"://ชดใช้ใบสำคัญ
            //        MoneyRequestDN1.Visible = false;
            //        MoneyRequestSalaries1.Visible = false;
            //        MoneyRequestLoan1.Visible = false;
            //        MoneyRequest1.Visible = true;
            //        MoneyRequest1.LoadMoneyRequest(strMoneyReqSeq, strBgmSourceSeq, strBgmYear);
            //        break;
            //    case "14"://เงินเดือน
            //        MoneyRequestDN1.Visible = false;
            //        MoneyRequestSalaries1.Visible = true;
            //        MoneyRequestLoan1.Visible = false;
            //        MoneyRequest1.Visible = false;
            //        MoneyRequestSalaries1.LoadMoneyRequestSalaries(strMoneyReqSeq, strBgmSourceSeq, strBgmYear);
            //        break;
            //    case "16"://เงินยืม
            //        MoneyRequestDN1.Visible = false;
            //        MoneyRequestSalaries1.Visible = false;
            //        MoneyRequestLoan1.Visible = true;
            //        MoneyRequest1.Visible = false;
            //        MoneyRequestLoan1.LoadMoneyRequestLoan(strMoneyReqSeq, strBgmSourceSeq, strBgmYear);
            //        break;
            //    case "17"://งบกลาง
            //        MoneyRequestDN1.Visible = false;
            //        MoneyRequestSalaries1.Visible = false;
            //        MoneyRequestLoan1.Visible = false;
            //        MoneyRequest1.Visible = false;
            //        break;
            //}
        }

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void ddlFilterWorkAreaSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void ddlFilterEventSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            lblBgmYear.Text = txtBgmYear.Text;
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        //==========================================================================
    }
}
